package com.bz.sunmipay.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.baizhi.baseapp.activity.BaseActivity;
import com.baizhi.baseapp.util.JsonUtils;
import com.bz.sunmipay.R;
import com.bz.sunmipay.adapter.SettingsAdapter;
import com.bz.sunmipay.controller.GlobalDataHolder;
import com.bz.sunmipay.model.SettingsInfo;
import com.bz.sunmipay.utils.IntentUtils;

import java.util.List;

/**
 * 设置
 * <p>
 * Created by liuyakui on 2019-09-07.
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_setting);
        setLeftContentIcon(R.mipmap.back);
        setRightContentIcon(R.mipmap.home);
        setCenterText("SETTINGS");

        initView();
    }

    private void initView() {
        ListView lv = findViewById(R.id.settings_list);
        final View footer = LayoutInflater.from(this).inflate(R.layout.settings_footer_layout, lv, false);
        footer.findViewById(R.id.logout).setOnClickListener(this);
        lv.addFooterView(footer);
        SettingsAdapter adapter = new SettingsAdapter(this);
        List<SettingsInfo> settings = JsonUtils.parseJson(this, "setting_config", SettingsInfo[].class);
        adapter.setItems(settings);
        lv.setAdapter(adapter);
    }

    @Override
    protected void handleClickEvent(int event) {
        super.handleClickEvent(event);
        if (event == RIGHT_BUTTON) {
            IntentUtils.goToHome(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logout:
                GlobalDataHolder.getInstance(this).logout();
                showMessage("logout success");
                IntentUtils.goToLoginPage(this);
                break;
        }
    }
}
